import 'package:flutter/material.dart';

class Product {
  final String title;
  final String description;

  Product(this.title, this.description);
}

///
/// 导航示例
///
class DemoNavigator2 extends StatelessWidget {
  const DemoNavigator2({Key? key}) : super(key: key);

  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      title: '导航的数据传递和接收',
      home: ProductListWidget(
        products: List.generate(20,
                (index) => Product('商品 $index', '这是一个商品详情，编号: $index'))
      ),
    );
  }
}

class ProductListWidget extends StatelessWidget {
  final List<Product> products;
  const ProductListWidget({Key? key, required this.products}) : super(key: key);

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(title: Text('商品列表'),),
      body: ListView.builder(
        itemCount: products.length,
        itemBuilder: (context, index){
          return ListTile(title: Text(products[index].title),
            onTap: (){
              Navigator.push(context,
                MaterialPageRoute(
                  builder: (context)=> ProductDetailWidget(product: products[index])
                )
              );
            },
          );
        }
      ),
    );
  }
}

class ProductDetailWidget extends StatelessWidget {
  final Product product;
  const ProductDetailWidget({Key? key, required this.product}) : super(key: key);

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(title: const Text('商品详情')),
      body: Center(
        child: Text(product.description),
      ),
    );
  }
}
